package engineer.seniordesign.cynogriffin;

import android.app.ActionBar;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;


public class SettingsActivity extends PreferenceActivity {
	private String host, port, username, password;
	private SharedPreferences settings;
	private EditTextPreference etpHost, etpPort, etpUsername, etpPassword;
	
	@SuppressWarnings("deprecation")
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        addPreferencesFromResource(R.xml.settings);
        
        // Action bar options if SDK is high enough
     	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
     		ActionBar actionBar = getActionBar();
     	   	actionBar.setDisplayHomeAsUpEnabled(true);
     	}
     	
     	etpHost = (EditTextPreference) findPreference("prefHost");
     	host = etpHost.getText();
     	etpHost.setSummary(host);
 
     	etpPort = (EditTextPreference) findPreference("prefPort");
     	port = etpPort.getText();
     	etpPort.setSummary(port);
     	
     	etpUsername = (EditTextPreference) findPreference("prefUsername");
     	username = etpUsername.getText();
     	etpUsername.setSummary(username);
  
     	etpPassword = (EditTextPreference) findPreference("prefPassword");
     	password = etpPassword.getText();
     	
     	
    }
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_app_view, menu);
		return true;
	}
	
	@SuppressWarnings("deprecation")
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.quit:
	        	
	        	// Close RDP connections
	        case android.R.id.home:
	         	settings = getSharedPreferences("settings", MODE_PRIVATE);
	        	SharedPreferences.Editor editor = settings.edit();
	         	
	        	etpHost = (EditTextPreference) findPreference("prefHost");
	         	host = etpHost.getText();
	         	editor.putString("prefHost", host);
	     
	         	etpPort = (EditTextPreference) findPreference("prefPort");
	         	port = etpPort.getText();
	         	editor.putString("prefPort", port);
	         	
	         	etpUsername = (EditTextPreference) findPreference("prefUsername");
	         	username = etpUsername.getText();
	         	editor.putString("prefUsername", username);
	      
	         	etpPassword = (EditTextPreference) findPreference("prefPassword");
	         	password = etpPassword.getText();
	         	editor.putString("prefPassword", password);
	         	
	         	editor.commit();
	        	Intent i = new Intent(getApplicationContext(), LinkFileListActivity.class);
	        	startActivity(i);
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
}
